## Class for Contact
## Author : Wenpeng WEI ( mailto: wenpeng.wei@gmail.com )
## 2008/11/19

require 'rexml/document'

class Contact
  
  attr_accessor :uri, :version, :user_id, :local_name, :buddy_lists, :relation_status, :feika_read_version, :online_notify, :charge_type, :nickname, :gender, :impresa, :nation, :province, :city, :ivr_enabled, :portrait_crc, :provisioning, :mobile_no, :name, :birth_date, :birthday_valid, :lunar_animal, :horoscope, :profile, :blood_type, :occupation, :hobby ,:type, :fetion_num
  
  def initialize buddy_xml    
    @uri = buddy_xml.attributes["uri"]
    @user_id = buddy_xml.attributes["user-id"]
    @local_name = buddy_xml.attributes["local-name"]
    @buddy_lists = buddy_xml.attributes["buddy-lists"]
    @relation_status = buddy_xml.attributes["relation-status"]
    @feika_read_version = buddy_xml.attributes["fetka-read-version"]
    @online_nodify = buddy_xml.attributes["online-notify"]
    if @uri =~ /sip:\d+@/  
      @fetion_num = @uri.scan(/sip:\d+@/)[0][4...-1] 
    else @uri =~ /tel:\d\d\d\d\d\d\d\d\d\d\d/
      @fetion_num = nil
      @mobile_no = @uri[4,11]
    end
  end
  
  def update buddy_xml
    xml = REXML::Document.new buddy_xml
    xml.elements.each("//contact") do |c|
      @uri = c.attributes["uri"]
    end
    xml.elements.each("//personal") do |p|
      @version = p.attributes["version"]
      @user_id = p.attributes["user-id"]
      @charge_type = p.attributes["charge-type"]
      @nickname = p.attributes["nickname"]
      @gender = p.attributes["gender"]
      @impresa = p.attributes["impresa"]
      @nation = p.attributes["nation"]
      @province = p.attributes["province"]
      @city = p.attributes["city"]
      @ivr_enabled = p.attributes["ivr-enabled"]
      @portrait_crc = p.attributes["portrait-crc"]
      @provisioning = p.attributes["provisioning"]
      @mobile_no = p.attributes["mobile-no"]
      @name = p.attributes["name"]
      @birth_date = p.attributes["birth-date"]
      @birthday_valid = p.attributes["birthday-valid"]
      @lunar_animal = p.attributes["lunar-animal"]
      @horoscope = p.attributes["horoscope"]
      @profile = p.attributes["profile"]
      @blood_type = p.attributes["blood-type"]
      @occupation = p.attributes["occupation"]
      @hobby = p.attributes["hobby"]
    end
  end
  
  def to_dis
    
    name = @mobile_no
    name = @fetion_num if @fetion_num && @fetion_num!=""
    name = @name if @name && @name !=""
    name = @nickname if @nickname && @nickname !=""
    name = @local_name if @local_name && @local_name !=""
    num = @mobile_no
    num = @fetion_num if @fetion_num && @fetion_num !=""

    str = "#{name}(@#{num})"
  end
  
  
end